package com.ben.multiple.poi;

import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * @author BenJin Yin
 * @date 2020/6/30
 */
public class LongResource {
    static final String URL_PREFIX = "http://localhost:9009/long";
    static TestRestTemplate restTemplate;

    @BeforeAll
    static void init() {
        restTemplate = new TestRestTemplate(new RestTemplateBuilder());
    }

    @Test
    void add() {
        String url = URL_PREFIX;
        Map<String, Object> body = new HashMap<>();
        body.put("id", 1);
        body.put("name", "b");
        body.put("time", null);
        body.put("size", null);
        HttpEntity<Object> httpEntity = new HttpEntity<>(body);
        ResponseEntity<Object> entity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Object.class);
        this.print(entity);
    }

    @Test
    void get() {
        String url = URL_PREFIX;
        ResponseEntity<Object> entity = restTemplate.getForEntity(url, Object.class);
        this.print(entity);
    }


    void print(ResponseEntity<?> entity) {
        if (entity == null || HttpStatus.OK != entity.getStatusCode()) {
            System.out.println(entity);
            return;
        }
        System.out.println(entity.getBody());
    }
}
